package com.luo.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luo.common.annotation.Log;
import com.luo.common.config.Constant;
import com.luo.common.controller.BaseController;
import com.luo.common.utils.PageUtils;
import com.luo.common.utils.Query;
import com.luo.common.utils.R;
import com.luo.partner.dao.model.TabManagerCommission;
import com.luo.partner.dao.model.TabMonthlyKnotTemp;
import com.luo.partner.service.TabManagerCommissionService;
import com.luo.partner.service.TabMonthlyKnotService;
import com.luo.system.domain.RoleDO;
import com.luo.system.domain.UserDO;

@RequestMapping("/sys/managerCommission")
@Controller
public class ManagerCommissionController extends BaseController {
	private String prefix="system/managerCommission";
	
	@Autowired
	TabManagerCommissionService tabManagerCommissionService;
	
	@RequiresPermissions("sys:managerCommission:managerCommission")
	@GetMapping("")
	String user(Model model) {
		return prefix + "/list";
	}

	@GetMapping("/list")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<TabManagerCommission> list = tabManagerCommissionService.findList(query);
		int total = tabManagerCommissionService.countList(query);
		PageUtils pageUtil = new PageUtils(list, total);
		return pageUtil;
	}

	@RequiresPermissions("checkApplyCommission")
	@Log("审核")
	@GetMapping("/apply/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("record", tabManagerCommissionService.get(id));
		return prefix+"/apply";
	}

	@RequiresPermissions("checkApplyCommission")
	@Log("审核")
	@PostMapping("/update")
	@ResponseBody
	R update(TabManagerCommission record) {
		if (getUser().getLevel() != 1) {
			return R.error("没有权限审核");
		}
		if (record.getCheckStatus() == null && record.getCheckStatus() != 0) {
			return R.error("数据有误");
		}
		
		try {
			tabManagerCommissionService.applyCommission(record.getId(), record.getCheckStatus());
		} catch (RuntimeException e) {
			return R.error("审核失败，" + e.getMessage());
		}
		
		return R.ok();
	}
}